package com.rcloud.server.sealtalk.controller;


import com.rcloud.server.sealtalk.controller.param.SubmitStepParam;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.DocListManager;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.util.N3d;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;


/**
 * 商户认证资料提交控制器
 */
@Api(tags = "商户认证资料提交相关接口")
@Slf4j
@RestController
@RequestMapping("/auth/data")
@AllArgsConstructor
public class AuthSubmitController extends BaseController {

    private DocListManager docListManager;


    @ApiOperation("证件上传的接口")
    @PostMapping("/submitFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "dataType", value = "上传文件类型 1身份证头像面 2身份证国徽面 3营业执照 4烟草证 5食品经营许可证", dataType = "Integer")
    })
    public APIResult<Object> submitFile(@RequestParam("file") MultipartFile file,
                                        @RequestParam("dataType") Integer dataType) throws IOException {
        if (file.isEmpty()) {
            return APIResultWrap.ok("");
        }
        Integer userId = getCurrentUserId();
        String fileName = docListManager.submitFile(userId,file,dataType);
        return APIResultWrap.ok("https://" + sealtalkConfig.getQiniuBucketDomain() + "/" + fileName);
    }

    /**
     * 商户资料提交（下一步）
     */
    @PostMapping(value = "/submitStep")
    @ApiOperation(value = "商户资料提交（下一步）", notes = "商户资料提交（下一步）")
    public APIResult<Object> submitStep(@Valid @RequestBody SubmitStepParam param) throws ServiceException, IOException {
        Integer userId = getCurrentUserId();
        Boolean dataAuth = docListManager.submitDataAuth(param, userId);
        return APIResultWrap.ok(dataAuth);
    }

    /**
     * 获取用户的认证状态
     */
    @PostMapping(value = "/queryAuthStatus")
    @ApiOperation(value = "获取用户的认证状态", notes = "获取用户的认证状态")
    public APIResult<Object> queryAuthStatus() throws ServiceException {
        Integer userId = getCurrentUserId();
        Map<String, Object> map = docListManager.queryAuthStatus(userId);
        return APIResultWrap.ok(map);
    }
    /**
     * 查看用户认证内容
     */
    @GetMapping(value = "/queryAuthContent")
    @ApiOperation(value = "查看用户认证内容", notes = "查看用户认证内容")
    @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "String")
    public APIResult<Object> queryAuthContent(@RequestParam String userId) throws ServiceException {
        Integer decode = N3d.decode(userId);
        Map<String, Object> map = docListManager.queryAuthContent(decode);
        return APIResultWrap.ok(map);
    }
}